package com.cruisecontrol4eclipse.core;

import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.PlatformUI;

public class GenerelPreferencePage extends PreferencePage implements
		IWorkbenchPreferencePage {

	public GenerelPreferencePage() {
	}

	public GenerelPreferencePage(String title) {
		super(title);
	}

	public GenerelPreferencePage(String title, ImageDescriptor image) {
		super(title, image);
	}

	protected Control createContents(Composite parent) {
		Composite cGeneral = new Composite(parent, 0);
		cGeneral.setLayout(new GridLayout(1, false));
		cGeneral.setLayoutData(new GridData(4, 4));

		Control c2 = createServerDataContents(cGeneral);
		c2.setLayoutData(new GridData(4, 1, true, false));
		Control c1 = createWebSVNContents(cGeneral);
		c1.setLayoutData(new GridData(4, 1, true, false));
		return cGeneral;
	}

	private Control createServerDataContents(Composite parent) {
		Group group = new Group(parent, 0);
		group.setLayout(new GridLayout(2, false));
		group.setText("Server Data");
		addInputField(group,"Server URL", PersistedData.HOSTNAME, "http://www.mycruisecontrolserver.com");
		addInputField(group,"Http Port", PersistedData.HTTP_PORT, "8080");
		addInputField(group,"Rmi Port", PersistedData.RMI_PORT, "1099");
		return group;
	}

	private Control createWebSVNContents(Composite parent) {
		Group group = new Group(parent, 0);
		group.setLayout(new GridLayout(2, false));
		group.setText("Web SVN Settings");
		addInputField(group,"Web SVN 'Blame' Url", PersistedData.WEB_SVN_BLAME_URL, "http://mysvnblameurl/");
		addInfoText(group, "Tags to use: "+ PersistedData.FROM_SVN_REV_TAG +  " " + PersistedData.TO_SVN_REV_TAG);
		addInputField(group,"Web SVN 'Revision' Url", PersistedData.WEB_SVN_REVISION_URL, "http://mysvnrevisionurl/");
		addInfoText(group, "Tags to use: "+ PersistedData.SVN_REV_MINUS_1_TAG +  " " + PersistedData.SVN_REV_TAG);
		return group;
	}

	private void addInfoText(Composite parent, String label) {
		Label lLabel = new Label(parent, SWT.NONE);
		lLabel.setLayoutData(new GridData(1, 16777216, false, false));
		Label lLabel2 = new Label(parent, SWT.NONE);
		GridData layoutData = new GridData(4, 4, true, true);
		layoutData.minimumWidth = 100;
		lLabel2.setLayoutData(layoutData);
		lLabel2.setText(label);
	}

	@Override
	public void init(IWorkbench workbench) {
	}

	public Text addInputField(Composite parent, String label,
			final String preferenceStoreID, String defaultValue) {
		Label lLabel = new Label(parent, SWT.NONE);
		lLabel.setText(label);
		lLabel.setLayoutData(new GridData(1, 16777216, false, false));

		Text tText = new Text(parent, SWT.BORDER);
		GridData layoutData = new GridData(4, 4, true, true);
		layoutData.minimumWidth = 100;
		tText.setLayoutData(layoutData);
		if (PlatformUI.getPreferenceStore().getString(preferenceStoreID)
				.equals(""))
			PlatformUI.getPreferenceStore().setValue(preferenceStoreID,
					defaultValue);

		tText.setText(PlatformUI.getPreferenceStore().getString(
				preferenceStoreID));
		tText.addFocusListener(new FocusListener() {
			public void focusGained(FocusEvent e) {
			}

			public void focusLost(FocusEvent e) {
				PlatformUI.getPreferenceStore().setValue(preferenceStoreID,
						((Text) e.widget).getText());
			}
		});
		return tText;
	}
}
